package kr.hkit.androidcomplete01.chapter08;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class LineLongClickEx extends Activity {

	ArrayList<Vertex> arVertex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		arVertex = new ArrayList<>();
		final MyView mv = new MyView(this);
		mv.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Toast.makeText(LineLongClickEx.this, "aaaa", Toast.LENGTH_SHORT)
						.show();
				return false;
			}
		});
		setContentView(mv);
	}

	class MyView extends View {

		Paint paint;

		public MyView(Context context) {
			super(context);

			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.BLACK);
			paint.setStrokeWidth(3);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			for (int i = 0; i < arVertex.size(); i++) {
				if (arVertex.get(i).Draw) {
					canvas.drawLine(arVertex.get(i - 1).x,
							arVertex.get(i - 1).y, arVertex.get(i).x,
							arVertex.get(i).y, paint);
				}
			}
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}

			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				arVertex.add(new Vertex(event.getX(), event.getY(), true));
				invalidate();
				return true;
			}
			return false;
		}

		

	}
}
